package model

import "ginProject/biz/dao"

type WarehouseProduct struct {
	ID           int64
	ProductId    int
	CurrentCnt   int
	WaitCnt      int
	RealityCnt   int
	CreateTime   string
	ModifiedTime string
}

/**
 * @Author leibidong
 * @Description 查询所有的库存信息
 * @Date  2021/7/6 19:19
 **/
func SelectAllWarehouse() []WarehouseProduct {
	db := dao.InitDB()
	var ware []WarehouseProduct
	db.Raw("select * from warehouse_product ").Scan(&ware)
	return ware
}

/**
 * @Author leibidong
 * @Description 根据商品id查询库存信息
 * @Date  2021/7/6 19:19
 **/
func SelectWarehouseProductById(id int) []WarehouseProduct {
	db := dao.InitDB()
	var ware []WarehouseProduct
	db.Raw("select * from warehouse_product where product_id=? ", id).Scan(&ware)
	return ware
}

/**
 * @Author leibidong
 * @Description 根据id查询商品的数目
 * @Date  2021/7/6 20:04
 **/
func SelectProductCnt(productId int) WarehouseProduct {
	db := dao.InitDB()
	var ware WarehouseProduct
	db.Raw("select current_cnt,id from warehouse_product where product_id=?  ", productId).Scan(&ware)
	return ware
}

/**
 * @Author leibidong
 * @Description 根据商品id更新商品库存数量
 * @Date  2021/7/6 20:11
 **/
func UpdateWareCurrentCnt(pId, cnt int) {
	db := dao.InitDB()
	db.Exec("update warehouse_product set current_cnt=? where product_id=?", cnt, pId)
}

/**
 * @Author leibidong
 * @Description 根据库存id更新商品库存数量
 * @Date  2021/7/6 21:03
 **/
func UpdateWareCurrentCntByWareId(id, cnt int) {
	db := dao.InitDB()
	db.Exec("update warehouse_product set current_cnt=? where id=?", cnt, id)
}

/**
 * @Author leibidong
 * @Description 创建新的商品数量记录
 * @Date  2021/7/6 20:21
 **/
func AddWareCurrentCnt(product WarehouseProduct) {
	db := dao.InitDB()
	db.Exec("insert into  warehouse_product(product_id,current_cnt,wait_cnt,reality_cnt,create_time,"+
		"modified_time) values(?,?,?,?,?,?)", product.ProductId, product.CurrentCnt, product.WaitCnt, product.RealityCnt,
		product.CreateTime, product.ModifiedTime)
}

/**
 * @Author leibidong
 * @Description 删除库存id对应的库存记录
 * @Date  2021/7/6 21:16
 **/
func DeleteWareProduct(id int) {
	db := dao.InitDB()
	db.Exec("delete from warehouse_product where id=?", id)
}

/**
 * @Author leibidong
 * @Description 查询是否存在id对应的库存记录
 * @Date  2021/7/6 21:19
 **/
func SelectWareproductById(id int) WarehouseProduct {
	db := dao.InitDB()
	var ware WarehouseProduct
	db.Raw("select id from warehouse_product where id=?", id).Scan(&ware)
	return ware
}
